/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.citytag.process;

import com.apt.bean.Cities;
import com.apt.countrytag.Process_PageCountry;
import java.util.HashMap;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bach
 */
public class CityTagHandler extends SimpleTagSupport {

    private String page;
    private String country_id;
    private String city_id;
    private String country_name;  /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        int cursor1;
        HashMap<Integer, Cities> hm = GetCity.getListCity(country_id);
        try {
            if (hm != null && hm.size() != 0) {
                out.println(Process_PageCountry.divPage(page, hm.size(), 16, country_name, country_id));
                int uid_f = 1;
                int uid_l = 30;

                try {
                    cursor1 = Integer.parseInt(page);

                } catch (NumberFormatException ex) {
                    cursor1 = 1;
                }
                if (cursor1 > 1) {
                    uid_f = (cursor1 - 1) * 30;
                    uid_l = cursor1 * 30;
                }
                int current_page = uid_l / 30;
                for (int i = uid_f; i < uid_l; i++) {
                    Cities u = hm.get(i);
                    if (u != null) {
                        int status = Integer.parseInt(u.getCity_Status());
                        String city_Name = u.getCity_Name();
                        String city_id=u.getCity_Id();
                        String country_ID = u.getCountry_Id();
                        StringBuilder sb = new StringBuilder(" <tr>");

                        if (status == 1) {
                            sb.append("<form action=\"EditCityServlet\" method=\"get\">\n"
                                    + " <td> \n"
                                    + "<input type=\"text\"  name=\"city_Name\" value=\"" + city_Name + "\"/>\n"
                                    + "<input type=\"submit\" class=\"button\" value=\"Update Name\"/>\n"
                                    + "<input type=\"hidden\" name=\"city_id\" value=\"" + city_id + "\"   />"
                                    + "<input type=\"hidden\" name=\"country_ID\" value=\"" + country_ID + "\"   />"
                                    + "<input type=\"hidden\" name=\"country_Name\" value=\"" + country_name + "\"   />"
                                    + "<input type=\"hidden\" name=\"pageno\" value=\"" + current_page + "\"   />"
                                    + " </td>\n"
                                    + "</form>");
                            sb.append("<td><a href=\"#\" title=\"title\">Enable</a></td>");
                        } else {
                            sb.append("<td>" + city_Name + "</td>");
                            sb.append("  <td><a href=\"#\" title=\"title\">Disable</a></td>");
                        }
                        sb.append("    <td>");
                        if (status == 1) {
                            sb.append("<a href=\"DisableCityServlet?pageno=" + current_page + "&cityname=" + city_Name + "&country_id=" + country_ID + "&country_Name="+country_name+ "\" title=\"Disable\"><img src=\"resources/images/icons/cross.png\" alt=\"Disable\" /></a>");
                        } else {
                            sb.append("<a href=\" Enable_Country_CityServlet?pageno=" + current_page + "&cityname=" + city_Name + "&country_id=" + country_ID +"&country_Name="+country_name+"\" title=\"Enable\"><img src=\"resources/images/icons/pencil.png\" alt=\"Enable\" /></a>");
                        }
                        sb.append("</td>");
                        sb.append("</tr>");
                        out.print(sb.toString());

                    }
                }
            }else{
                System.out.println("so luong ci ty cua quoc gia la:"+hm.size());
                 out.print(hm.size());
            }
            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

        } catch (java.io.IOException ex) {
            throw new JspException("Error in ProfessionnalTagHandler tag", ex);
        }
    }

    public void setPage(String page) {
        this.page = page;
    }

    public void setCountry_id(String country_id) {
        this.country_id = country_id;
    }

    public void setCity_id(String city_id) {
        this.city_id = city_id;
    }

    /**
     * @return the country_name
     */
    public String getCountry_name() {
        return country_name;
    }

    /**
     * @param country_name the country_name to set
     */
    public void setCountry_name(String country_name) {
        this.country_name = country_name;
    }
}
